package com.test.qpbgboot.backend.service.impl;

import com.test.qpbgboot.backend.mapper.BackendAccountMapper;
import com.test.qpbgboot.backend.mapper.OperationLogMapper;
import com.test.qpbgboot.backend.pojo.dto.BackendAccountLoginParam;
import com.test.qpbgboot.backend.pojo.dto.BackendAccountQuery;
import com.test.qpbgboot.backend.pojo.dto.BackendAccountSaveParam;
import com.test.qpbgboot.backend.pojo.dto.OperationLogQuery;
import com.test.qpbgboot.backend.pojo.entity.BackendAccount;
import com.test.qpbgboot.backend.pojo.vo.BackendAccountVO;
import com.test.qpbgboot.backend.pojo.vo.OperationLogVO;
import com.test.qpbgboot.backend.service.BackendService;
import com.test.qpbgboot.backend.service.MerchantService;
import com.test.qpbgboot.base.enums.GeneralStatusEnum;
import com.test.qpbgboot.base.exception.ServiceException;
import com.test.qpbgboot.base.response.ResultStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class BackendServiceImpl implements BackendService {

    @Autowired
    private BackendAccountMapper backendAccountMapper;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private OperationLogMapper operationLogMapper;

    @Override
    public List<BackendAccountVO> backendAccountSelect(
            BackendAccountQuery backendAccountQuery) {
        log.debug("后台账号查询业务，参数:{}", backendAccountQuery);
        List<BackendAccountVO> backendAccountVOList =
                backendAccountMapper.selectVObyQuery(backendAccountQuery);
        log.debug("后台账号查询结果:{}", backendAccountVOList);
        return backendAccountVOList;
    }

    @Override
    public void backendAccountSave(BackendAccountSaveParam backendAccountSaveParam) {
        log.debug("后台账号保存业务，参数:{}", backendAccountSaveParam);
        BackendAccount backendAccount = new BackendAccount();
        BeanUtils.copyProperties(backendAccountSaveParam, backendAccount);

        List<String> statusAndType = Arrays.asList("0", "1");
        if (!statusAndType.contains(backendAccount.getStatus())
                || !statusAndType.contains(backendAccount.getType())) {
            throw new ServiceException(ResultStatus.ILLEGAL_ARGUMENT, "后台账号状态或者类型不正确");
        }

        if (backendAccountSaveParam.getId() == null) {
            backendAccountMapper.insert(backendAccount);
        } else {
            backendAccountMapper.update(backendAccount);
        }
    }

    @Override
    public void backendAccountLogOff(Long accountId) {
        log.debug("后台账号注销业务，参数:{}", accountId);
        BackendAccountQuery backendAccountQuery = new BackendAccountQuery();
        backendAccountQuery.setId(accountId);
        List<BackendAccountVO> backendAccountVOList =
                backendAccountMapper.selectVObyQuery(backendAccountQuery);
        if (backendAccountVOList != null && !backendAccountVOList.isEmpty()) {
            if (backendAccountVOList.get(0).getStatus()
                    .equals(GeneralStatusEnum.DISABLE.getValue())) {
                throw new ServiceException(ResultStatus.INVALID_ID, "该账号已经禁用");
            }
            Long merchantId = backendAccountVOList.get(0).getMerchantId();
            merchantService.merchantLogOff(merchantId);
        } else {
            throw new ServiceException(ResultStatus.INVALID_ID, "后台账号id无效");
        }
        BackendAccount backendAccount = new BackendAccount();
        backendAccount.setId(accountId);
        backendAccount.setStatus(GeneralStatusEnum.DISABLE.getValue());
        backendAccountMapper.changeStatus(backendAccount);
    }

    @Override
    public List<OperationLogVO> operationLogSelect(OperationLogQuery operationLogQuery) {
        log.debug("操作日志查询业务，参数:{}", operationLogQuery);
        List<OperationLogVO> operationLogVOList = operationLogMapper
                .selectVObyQuery(operationLogQuery);
        log.debug("操作日志查询结果:{}", operationLogVOList);
        return operationLogVOList;
    }

    @Override
    public BackendAccountVO backendAccountLogin(
            BackendAccountLoginParam backendAccountLoginParam) {
        log.debug("后台账号登录业务 入参:{}", backendAccountLoginParam);

        String username = backendAccountLoginParam.getUsername();
        String password = backendAccountLoginParam.getPassword();
        String password_check =
                backendAccountMapper.selectPasswordByUsername(username);
        if (password_check == null) {
            throw new ServiceException(ResultStatus.USERNAME_ERROR);
        }
        if (password.equals(password_check)) {
            BackendAccountQuery backendAccountQuery = new BackendAccountQuery();
            backendAccountQuery.setUsername(username);
            BackendAccountVO backendAccountVO =
                    backendAccountMapper.selectVObyQuery(backendAccountQuery)
                            .get(0);
            if (Objects.equals(backendAccountVO.getStatus(),
                    GeneralStatusEnum.DISABLE.getValue())) {
                throw new ServiceException(
                        ResultStatus.INCORRECT_STATUS, "用户不可用");
            }
            backendAccountMapper.loginCount(backendAccountVO.getId());
            log.debug("后台账号登录业务 入参:{},出参:{}",
                    backendAccountLoginParam, backendAccountVO);
            return backendAccountVO;
        } else {
            throw new ServiceException(ResultStatus.PASSWORD_ERROR);
        }
    }
}
